/*
 * Line.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.pi.common.plot.drawables

class Line( val line: org.pi.common.geom.Line) extends Drawable with org.pi.common.observerpattern.ObservableImpl { // TODO with Observable
  private var stroke : java.awt.Stroke = null
  def this(p1: org.pi.common.geom.Point, p2: org.pi.common.geom.Point) = this( org.pi.common.geom.Line(p1,p2) )
  def this(x1: Float,y1: Float, x2:Float, y2:Float)  = this( org.pi.common.geom.Point(x1,y1), org.pi.common.geom.Point(x2,y2) )

  private var _lineWidth : Float = 0
  def lineWidth = _lineWidth
  def lineWidth_=(w: Float) = {
    stroke = new java.awt.BasicStroke(w)
    _lineWidth = w
    notifyObservers()
  }
  this.lineWidth = Drawable.lineWidth

  private var _color = Drawable.paintColor
  def color = _color
  def color_=(c: java.awt.Color) = {
	  _color = c
	  notifyObservers()
  }

  override def draw( f: (Float, Float) => (Float, Float), graphics : java.awt.Graphics2D ) {
    graphics.setColor(color)
    graphics.setStroke(stroke)
    val (x1,y1) = f(line.p1.x, line.p1.y)
    val (x2,y2) = f(line.p2.x, line.p2.y)
    graphics.draw( new java.awt.geom.Line2D.Float( x1,y1,x2,y2 ))
  }
  override def getMinXyMaxXy() =
    (Math.min(line.p1.x,line.p2.x), 
     Math.min(line.p1.y,line.p2.y),
     Math.max(line.p1.x,line.p2.x),
     Math.max(line.p1.y,line.p2.y))
}
